package com.gopay.common.user.dao.impl;

import com.gopay.common.base.dao.impl.BaseReadDAOImpl;
import com.gopay.common.cipher.utils.SensitiveInfoUtils;
import com.gopay.common.constants.cust.CustCorpInfoConstants;
import com.gopay.common.constants.cust.UserInfoConstants;
import com.gopay.common.user.dao.CustPosConfigQueryDAO;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author lij-jiang
 * @ClassName: CustPosConfigQueryDAOImpl.java
 * @Description: 商户pos机配置DaoImpl
 * @date 2017年10月25日 17:12
 */
@Repository(value = "custPosConfigQueryDAO")
public class CustPosConfigQueryDAOImpl extends BaseReadDAOImpl<Object[], Long> implements CustPosConfigQueryDAO {
    @Override
    public int getPosConfigCount(Map<String, Object> params) {
        StringBuilder sql = new StringBuilder(" select count(*) from CUST_POS_CONFIG cpc");
        sql.append(" LEFT JOIN CUST_CORP_INFO cci ON cpc.cust_id = cci.cust_id WHERE cpc.status = '1' ");

        Map<String, Object> param = new HashMap<>();
        if (params != null) {
            Map<String, Object> map = initSql(params, sql);
            param = (Map<String, Object>) map.get("param");
            sql = (StringBuilder) map.get("sql");
        }

        return findCountBySql(sql.toString(), param);
    }

    @Override
    public List<Object[]> getPosConfigList(Map<String, Object> params, Integer page, Integer rowNum) {

        StringBuilder sql = new StringBuilder(" SELECT cci.cust_id, cci.corp_name, cci.email, cci.cust_mob," +
                " cpc.shop_name, cpc.pos_no, cpc.id, cpc.created_ts FROM CUST_POS_CONFIG cpc ");
        sql.append(" LEFT JOIN CUST_CORP_INFO cci ON cpc.cust_id = cci.cust_id ");
        sql.append(" WHERE cpc.status = '1' ");

        Map<String, Object> param = new HashMap<>();
        if (params != null) {
            Map<String, Object> map = initSql(params, sql);
            param = (Map<String, Object>) map.get("param");
            sql = (StringBuilder) map.get("sql");
        }

        sql.append(" order by cci.cust_id,cpc.id desc ");

        return findBySql(sql.toString(), param, page, rowNum);
    }

    @Override
    public int getCorpCount(Map<String, Object> params) {
        StringBuilder sb = new StringBuilder("SELECT count(*) FROM cust_corp_info cci ");
        sb.append(" INNER JOIN user_info u ON cci.cust_id = u.cust_id ");
        sb.append(" WHERE cci.is_real_name_certify = '" + CustCorpInfoConstants.REAL_NAME_CERTIFY_YES + "' "
                + " and cci.corp_stat = '"+ CustCorpInfoConstants.CORP_STAT_NORMAL + "'"
                + " and u.oper_id = '" + UserInfoConstants.CORP_ADMIN_CODE + "' ");

        Map<String, Object> param = new HashMap<>();


        if (params != null) {
            Map<String, Object> map = initSql(params, sb);
            param = (Map<String, Object>) map.get("param");
            sb = (StringBuilder) map.get("sql");
        }
        return findCountBySql(sb.toString(), param);
    }

    @Override
    public List<Object[]> getCorpList(Map<String, Object> params, Integer page, Integer rowNum) {
        StringBuilder sb = new StringBuilder("SELECT cci.cust_id, cci.corp_name, cci.email, cci.cust_mob FROM cust_corp_info cci ");
        sb.append(" INNER JOIN user_info u ON cci.cust_id = u.cust_id ");
        sb.append(" WHERE cci.is_real_name_certify = '" + CustCorpInfoConstants.REAL_NAME_CERTIFY_YES + "' "
                + " and cci.corp_stat = '"+ CustCorpInfoConstants.CORP_STAT_NORMAL + "'"
                + " and u.oper_id = '" + UserInfoConstants.CORP_ADMIN_CODE + "' ");

        Map<String, Object> param = new HashMap<>();


        if (params != null) {
            Map<String, Object> map = initSql(params, sb);
            param = (Map<String, Object>) map.get("param");
            sb = (StringBuilder) map.get("sql");
        }

        sb.append(" order by cci.cust_id desc");
        return this.findBySql(sb.toString(), param, page, rowNum);
    }

    private Map<String, Object> initSql(Map<String, Object> params, StringBuilder sql) {

        Map<String, Object> map = new HashMap<>();
        Map<String, Object> param = new HashMap<>();

        if (params.get("corpName") != null) {
            sql.append(" and cci.corp_name like :corpName");
            param.put("corpName", params.get("corpName"));
        }

        if (params.get("email") != null) {
            sql.append(" and cci.email like :email");
            param.put("email", params.get("email"));
        }

        if (params.get("custId") != null) {
            sql.append(" and cci.cust_id = :custId ");
            param.put("custId", params.get("custId"));
        }

        if (params.get("custMob") != null) {
            sql.append(" and cci.cust_mob = :custMob ");
            param.put("custMob", SensitiveInfoUtils.smartEncryptWithCheck((String) params.get("custMob")));
        }

        if (params.get("shopName") != null) {
            sql.append(" and cpc.shop_name like :shopName");
            param.put("shopName", "%" + params.get("shopName") + "%");
        }

        if (params.get("posNo") != null) {
            sql.append(" and cpc.pos_no = :posNo ");
            param.put("posNo", params.get("posNo"));
        }

        if (params.get("id") != null) {
            sql.append(" and cpc.id = :id");
            param.put("id", params.get("id"));
        }
        map.put("param", param);
        map.put("sql", sql);

        return map;

    }
}